//var objListType={""}
var nid = "1";
var data = {};
var basepath=''//需修改

var objectToNews={
		"title" : "title",
		"from" : "source",
		"textLink" : "newsUrl",
//		"brief":"abstractContent"
}
var newsListUrl = {
	"1" : {
		"type" : "json",
		"SZ" : "http://qc.wa.news.cn/nodeart/list?nid=113352&pgnum=1&cnt=10&tp=1&orderby=1",
		"JY" : "http://qc.wa.news.cn/nodeart/list?nid=11109063&pgnum=1&cnt=10&tp=1&orderby=1",
		"url" : basepath+"/jsoup/getByUrl",
		"getMethod" : ".data.list" // data.obj.data.list 获取list列表格式
	},
	"2" : {
		"type" : "obj",
		"SZ" : "http://china.zjol.com.cn/gnxw/",
		"JJ" : "http://fin.zjol.com.cn/",
		"RS" : " http://zjnews.zjol.com.cn/rsdt/zxrm/",
		"url" : basepath+"/jsoup/getZheJiangNews",
		"getMethod" : ""
	},

	"3" : {
		"type" : "obj",
		"SZ" : "http://politics.gmw.cn/node_9840.htm",
		"JY" : "http://edu.gmw.cn/node_10810.htm",
		"url" :basepath+ "/jsoup/getGuangMingNews",
		"getMethod" : ""
	}
}
// "1"、新华网
var radioList = {
	"1" : {
		"SZ" : "时政",
		"JY" : "教育"
	},
	"2" : {
		"SZ" : "时政",
		"JJ" : "经济",
		"RS" : "人事"
	},
	"3" : {
		"SZ" : "时政",
		"JY" : "教育"
	}
}
// 请求列表返回实体类结构
var objectList = {
	"1" : {
		"title" : "Title",
		"pubTime" : "PubTime",
		"orgin" : "SourceName",
		"url" : "LinkUrl"
	},
	"2" : {
		"title" : "title",
		"pubTime" : "pubTime",
		"orgin" : "source",
		"url" : "newsUrl"
	},
	"3" : {
		"title" : "title",
		"pubTime" : "pubTime",
		"orgin" : "source",
		"url" : "newsUrl"
	}
}
// 爬虫抓取
function initjsoup() {
	if (!newsGet) {
		$(".an-addNewsRight").hide();
		return;
	}
	initRadio("1");
	// 下拉框切换设定
	$("#jsoup").change(function() {
		var selectId = $(this).find("option:selected").attr("id");
		nid = selectId;
		initRadio(selectId);
	})
}

// 获取名字 SZ JY等
var defaultName = "";
// 初始化 根据selectId
function initRadio(nid) {
	var num = 0;
	var list = radioList[nid];
	var htmls = "";

	for (i in list) {
		htmls += "<input type='radio' name='type' typeName='" + i + "' nid='"
				+ nid + "' " + getchecked(num, i, defaultName) + ">" + list[i];
		num++;
	}
	$("#selectRadio").html(htmls);

	$(":radio").click(function() {
		defaultName = $(this).attr("typeName");
		initlist(nid, defaultName);
	});
	// 初始化列表
	initlist(nid, defaultName);
}

// 初始化列表
function initlist(nid, typeName) {
	data.url = newsListUrl[nid][typeName];
	var url = newsListUrl[nid]["url"]
	AjaxSubmit("GET", data, url, cmbineList);
}
// 判断是否为第一个
function getchecked(num, i) {
	if (num == 0) {
		defaultName = i;
	}
	return num == 0 ? "checked" : "";
}

function cmbineList(data) {
	var method = newsListUrl[nid]["getMethod"];
	data = data.obj;
	if (newsListUrl[nid]["type"] === "json") {
		// data = jQuery.parseJSON(data)
		data = eval(data);
	}
	var list = eval("data" + method);
	var htmls = "";
	// 获取对象中的列表对象
	list.forEach(function(val, index) {
		var object = {};
		// 遍历爬取数据
		for (v in val) {
			// 拿出相应nid的实体类型
			var objectData = objectList[nid];
			for (obj in objectData) {
				// 如果字段匹配 赋值
				if (v === objectData[obj]) {
					eval("object." + obj + "=val[v]");
				}
			}
		}
		htmls += demoModule(object);
	})

	$("#contentList").html(htmls);
}

/**
 * 组装module
 * 
 * @param pubtime
 * @param orgin
 * @param title
 * @param url
 * @returns
 */
function demoModule(obj) {
	var htmls = "<div class='catchNewsList fontyahei'>";
	htmls += "<p class='catchnewsTime'>" + obj.pubTime + " 来源： " + obj.orgin
			+ "</p>";

	htmls += "<p class='catchNewsT'>" + obj.title + "</p>";
	htmls += "<a target='_blank' class='colorBlue' href='" + obj.url
			+ "'>原文链接</a> ";
	htmls += "<div onclick='pushData(\""+nid+"\",\""+obj.url+"\")'><span class='colorBlue cursorP' >抓取</span></div></div>";
	return htmls;
}
function pushData(nid,url){
	var data={"url":url,"type":nid}
	AjaxSubmit("GET", data, basepath+"/jsoup/getDataByType", combineSingleInpuData);
}

//组装单个实体数据给input
function combineSingleInpuData(data){
//	$("#brief").val(tempData.)
	var obj=data.obj;
	for(i in objectToNews){
		eval("$('#" + i + "').val(data.obj." + objectToNews[i] + ")");
	}
	editor.txt.html(data.obj.text);
}